package com.abigdreamer.im.client.module.network.handler;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import com.abigdreamer.im.client.module.login.EIMLoginWindow;
import com.abigdreamer.im.client.module.network.socket.ConnectSession;
import com.abigdreamer.im.client.module.talk.cinema.ClientReceivedMessageProcessManager;
import com.abigdreamer.im.client.ui.trayIcon.EIMTrayIcon;
import com.abigdreamer.im.client.util.EIMClientConfig;
import com.abigdreamer.im.client.util.EIMLogger;
import com.abigdreamer.im.message.BaseMessage;

/**
 * 
 */
public class ClientChatHandler extends IoHandlerAdapter {

	/**
	 * 当会话开始时被触发
	 */
	public void sessionOpened(IoSession ioSession) throws Exception {
		ConnectSession clientConnect = ConnectSession.getInstance();
		EIMLogger.INFO("会话已经打开");
		clientConnect.setTextSession(ioSession);
		clientConnect.sendTextMessage(EIMLoginWindow.getInstance().getLoginMessage());
	}

	/**
	 * 当接收到消息后被触发
	 */
	public void messageReceived(IoSession ioSession, Object msg) throws Exception {
		BaseMessage message = (BaseMessage) msg;
		EIMLogger.INFO("接受到得消息为：" + message);
		ClientReceivedMessageProcessManager.processMessage(message);
	}

	/**
	 * 当会话关闭时被触发
	 * 
	 */
	public void sessionClosed(IoSession ioSession) throws Exception {
		EIMLogger.Error("与服务器断开连接！！");
		EIMTrayIcon trayIcon = EIMTrayIcon.getInStance();
		trayIcon.showIcon(EIMClientConfig.OffLineTryIcon_Type);
	}

	/**
	 * 
	 * 当接口中其他方法抛出异常未被捕获时触发此方法
	 */
	public void exceptionCaught(IoSession ioSession, Throwable throwable) throws Exception {
	}

	/**
	 * 当发送消息后被触发
	 * 
	 */
	public void messageSent(IoSession ioSession, Object msg) throws Exception {
	}

	/**
	 * 当会话创建时被触发
	 * 
	 */
	public void sessionCreated(IoSession ioSession) throws Exception {

	}

	/**
	 * 
	 * 当会话空闲时被触发
	 */
	public void sessionIdle(IoSession ioSession, IdleStatus msg) throws Exception {

	}

}
